package com.sanxinsaas.task.framework.witght

import android.content.Context
import android.graphics.Rect
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import com.sos.widget.app.DisplayUtil

class KeyboardHelper(
    private val context: Context,
    val rootView: ViewGroup,
    vararg editTexts: EditText,
) : ViewTreeObserver.OnGlobalLayoutListener {

    lateinit var editTextList: MutableList<EditText>

    init {
        editTextList = editTexts.toMutableList()
    }

    private val imm: InputMethodManager =
        context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    private var keyboardHeight = 0
    private val editTextListY = mutableListOf<Int>()

    init {
        for (editText in editTextList) {
            editTextListY.add(editText.y.toInt())
            editText.setOnFocusChangeListener { view, hasFocus ->
                if (hasFocus) {
                    keyboardHeight = getKeyboardHeight()
                    if (keyboardHeight > 0) {
                        handleKeyboardVisible(view)
                    }else{
                        keyboardHeight=-keyboardHeight
                    }
                }
            }
        }
        rootView.viewTreeObserver.addOnGlobalLayoutListener(this)
    }

    override fun onGlobalLayout() {
        for (i in editTextList.indices) {
            val editText = editTextList[i]
            if (editText.isFocused) {
                //该输入view距离底部还有多少
                val viewLocation = IntArray(2)
                editText.getLocationOnScreen(viewLocation);
                val windowYMargin = viewLocation[1] + editText.height
                val appScreenHeight = DisplayUtil.getWindowsHeight(editText.context)
                val heightDifference = appScreenHeight - windowYMargin
                if (heightDifference < keyboardHeight) {
                    val rect = Rect()
                    rootView.getWindowVisibleDisplayFrame(rect)
                    val screenHeight = rootView.scrollY+(keyboardHeight-heightDifference)+editText.height
                    rootView.scrollTo(0, screenHeight)
                }
                break
            }
        }
    }

    fun showKeyboard(editText: EditText) {
        editText.requestFocus()
        imm.showSoftInput(editText, 0)
    }

    fun hideKeyboard(editText: EditText) {
        imm.hideSoftInputFromWindow(editText.windowToken, 0)
    }

    fun release() {
        rootView.viewTreeObserver.removeOnGlobalLayoutListener(this)
    }

    private fun handleKeyboardVisible(view: View) {

        val screenHeight = rootView.height
        for (i in editTextList.indices) {
            val editText = editTextList[i]
            if (editText.isFocused) {
                val editTextY = editTextListY[i]
                if (editTextY + editText.height > screenHeight - keyboardHeight) {
                    rootView.post {
                        val scrollY = editTextY + editText.height - rootView.height
                        rootView.scrollTo(0, scrollY)
                    }
                }
                break
            }
        }
    }

    private fun getKeyboardHeight(): Int {
        val resources = context.resources
        val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
        val navigationBarHeight =
            if (resourceId > 0) resources.getDimensionPixelSize(resourceId) else 0
        val rect = Rect()
        rootView.getWindowVisibleDisplayFrame(rect)
        val screenHeight = rootView.height
        val keyboardHeight = screenHeight - rect.bottom - navigationBarHeight
        if (keyboardHeight > 0) {
            this.keyboardHeight = keyboardHeight
        }
        return keyboardHeight
    }
}
